#!/usr/bin/env python

import time
import os.path

from activist.Config import Config
from activist.DataStore import DataStore
from activist.Person import Person
from activist.Group import Group
from activist.Place import Place
from activist.Event import Event
from activist.Alias import Alias
from activist.Comment import Comment
from activist.GenericData import GenericData
from activist.EventInformation import EventInformation
from activist.MetaData import MetaDataFactory, MetaData
from activist.CommandProcessor import CommandProcessor
import activist.Place
import activist.Person
import activist.Event
import activist.Time
import activist.Category

CREATE = False
BASE = '/home/mike/projects/activist.ca/grant-data-dump/actnetdev-2005-01-15/'
if CREATE:
	CommandProcessor.loadAndProcess()

import os
##listing = os.path.join(BASE,'listing.txt')

idToSite = {}
for line in open(os.path.join(BASE,'listing_site.txt'),'r').readlines():
	things = line.split('\t')
	id = things[0] + '-' + things[1]
	idToSite[id] = things[3]

listing = {}
for line in open(os.path.join(BASE,'listing.txt'),'r').readlines():
    things = line.split('\t')
    id = things[0] + '-' + things[1]
    for x in range(0,len(things)):
        if things[x].strip() == '\\N':
            things[x] = ''
    record = {
        'key': id,
        'country': things[6],
        'province': things[7],
        'city': things[8],
        'private': things[11],
        'type': things[14],
        'subtype': things[15],
        'category': things[16],
        'date_added': things[17],
        'date_start': things[18],
        'date_end': things[19],
        'deadline': things[20],
        'time_start': things[21],
        'time_end': things[22],
        'repeat_type': things[23],
        'repeat_number': things[24],
        'repeat_period': things[25],
        'repeat_frequency': things[26],
        'repeat_day': things[27],
        'repeat_date': things[28],
        'repeat_month': things[29],
        'repeat_nextdate': things[30],
        'wheelchair': things[31],
        'smoking': things[32],
        'pets': things[33],
        'signlanguage': things[34],
        'children': things[35],
        'genderrestrict': things[36],
        'address': things[37] + '\n' + things[38],
        'address_city': things[39],
        'address_province': things[40],
        'address_country': things[41],
        'postalcode': things[42],
        'address_mapurl': things[43],
        'capacity': things[44],
        'longitude': things[45],
        'latitude': things[46],
        'publicly_available': things[47]
        }
    listing[id] = record


join = {}
for line in open(os.path.join(BASE,'listing_listing.txt'),'r').readlines():
    things = line.split('\t')
    key = things[0] + '-' + things[1]
    value = things[2] + '-' + things[3]
    relation = things[4]
    if join.has_key(key):
        join[key].append((value,relation))
    else:
        join[key] = [(value,relation)]

detail = {}
notforme = {}
unclaimed = {}
if 1:
    eat = False
    data = open(os.path.join(BASE,'listingdetail.txt'),'r').read()
    newdata = filter(lambda a: a != '\r', data)

    line = ''
    for l in newdata.split('\n'):
        if len(l) == 0: continue
        if eat:
            line = line + l + '\n'
            eat = False
            continue

        if l[-1] == '\\':
            eat = True
            line = line + l[:-1] + '\n'
            continue

        line = line + l
        things = line.split('\t')
        key = things[0] + '-' + things[1]
	if len(things) < 31:
		print things[0] + '-' + things[1]
		_exit(1)
        if things[30] == '0': things[30] = ''
        if things[31] == '0': things[31] = ''
        list =  {
            'key': key,
            'title':things[8],
            'subtitle':things[9],
            'url':things[10],
            'description':things[13],#.decode('latin-1'), # "content"
            'mime':things[14],
            'short':things[15],#things[15].decode('latin-1'),	 
            'notes':things[16],
            'repeat-exception':things[19],
            'cost':things[20],
            'agerestriction':things[21],
            'languages':things[22],
            'address':things[23],
            'person':things[24],
            'phone0':things[25],
            'phone1':things[26],
            'fax':things[27],
            'email':things[28],
            'image_url':things[29],
            'image_width':things[30],
            'image_height':things[31],
            
            'posted_by':things[35]
            }
	for k in list:
		if list[k] == "\\N":
			list[k] = ''
	for (k,x) in list.items():
		if x is None:
			print "BAD BAD:",k

	detail[key] = list
	if not idToSite.has_key(key):
		unclaimed[key] = list
	else:
	    if idToSite[key] == 'www' or idToSite[key] == 'calgarydollars':
		    detail[key] = list
	    else:
		    stuff = []
		    if notforme.has_key(idToSite[key]):
			    stuff = notforme[idToSite[key]]
		    else:
			    notforme[idToSite[key]] = stuff
		    #stuff.append((key,list))
		    stuff.append(key)
        line = ""

for x in notforme.keys():
	print "notforme",x,len(notforme[x])#,notforme[x]
print len(unclaimed),"total un-mapped posts"

print len(detail),"total records for me"
people = {}
for x in detail.values():
    id = x['key']
    if not listing.has_key(id):
        print "WARNING:",id,"not in listing table"
        continue

    ty = listing[id]['type']
    more = listing[id]

    if ty == 'place':
        print "PLACE",x['title'],more

for x in detail.values():
    id = x['key']
    if not listing.has_key(id):
        print "WARNING:",id,"not in listing table"
        continue

    ty = listing[id]['type']
    more = listing[id]

    if ty == 'contact':
        if CREATE:
            realname = x['title'].strip()
            if realname == 'Contact...':
                continue
            if realname == '':
                continue

            if people.has_key(realname):
                person = people[realname]
                person.edit(None,
                            realname,
                            [x['phone0'],x['phone1'],x['fax']],
                            x['email'],
                            more['address'],
                            x['description'],
                            priv)
                continue
            
            person = activist.Person.Person()
            priv = more['private']
            if priv.strip().lower() == 'y':
                priv = True
            else:
                priv = False
            person.create(realname,
                          None,
                          None,
                          [x['phone0'],x['phone1'],x['fax']],
                          x['email'],
                          more['address'],
                          x['description'],
                          priv)
            people[realname] = person
        continue
                      

for x in detail.values():
    id = x['key']
    if not listing.has_key(id):
        print "WARNING:",id,"not in listing table"
        continue

    ty = listing[id]['type']
    more = listing[id]

    if ty == 'event':
        if 1:#CREATE:

            if more['date_added'] == '0000-00-00':
                continue
            
            person = None
            when = activist.Time.fromImportListingDetail(more)

            if when is None:
                print "CAN'T PARSE TIME"
                for x in more.keys():
                    if x.find('date') >= 0 or x.find('repeat') >= 0 or x.find('time') >= 0:
                        print x,': ',more[x]
                raise "uh oh"

            print "EVENT-TIME:",when
            access = []
            if more['signlanguage'].strip().lower() == 'y': access.append('signlanguage')
            if more['children'].strip().lower() == 'y': access.append('childcare')
            ## FIXME: wheelchair, smoking, pets, genederrestrict

            place = None
            contacts = []

            event = activist.Event.Event()
            event.create(
                person,
                x['title'],
                when,
                access,
                x['description'],
                x['short'],
                place,
                more['subtype'],
                contacts
                )
                

